<script lang="ts" setup>
import PictureUpload from "@/components/PictureUpload.vue";
import UrlPictureUpload from "@/components/UrlPictureUpload.vue";
import { onMounted, reactive, ref } from "vue";
import { message } from "ant-design-vue";
import {
  editPictureUsingPost,
  getPictureVoByIdUsingGet,
  listPictureTagCategoryUsingGet,
  uploadPictureBatchUsingPost
} from "@/api/pictureController";
import { useRoute, useRouter } from "vue-router";

type UploadType = 'file' | 'url';

const router = useRouter();
const route = useRoute();
const uploadType = ref<UploadType>('file');
const picture = ref<API.PictureVO>();
const pictureForm = reactive<API.PictureEditRequest>({});
const categoryOptions = ref<string[]>([]);
const tagOptions = ref<string[]>([]);
const formVisible = ref(false);

// 上传成功处理
const onSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture;
  Object.assign(pictureForm, {
    name: newPicture.name,
    introduction: newPicture.introduction,
    category: newPicture.category,
    tags: newPicture.tags
  });
  formVisible.value = true;
  message.success('图片上传成功！');
};

// 表单提交
const handleSubmit = async (values: any) => {
  loading.value = true;
  const res = await uploadPictureBatchUsingPost({
    ...formData,
  })
  if (res.data.code === 0 && res.data.data) {
    message.success(`创建成功，共 ${res.data.data} 条`)
    router.push({
      path: '/home',
    })
  } else {
    message.error('创建失败，' + res.data.message)
  }
  loading.value = false;
}


// 获取标签分类
const getTagCategoryOptions = async () => {
  try {
    const res = await listPictureTagCategoryUsingGet();
    if (res.data.code === 0 && res.data.data) {
      tagOptions.value = res.data.data.tagList || [];
      categoryOptions.value = res.data.data.categoryList || [];
    }
  } catch (e) {
    message.error('获取分类标签失败');
  }
};

// 获取已有图片数据
const getOldPicture = async () => {
  const id = route.query?.id;
  if (!id) return;

  try {
    const res = await getPictureVoByIdUsingGet({ id });
    if (res.data.code === 0 && res.data.data) {
      const data = res.data.data;
      picture.value = data;
      Object.assign(pictureForm, {
        name: data.name,
        introduction: data.introduction,
        category: data.category,
        tags: data.tags
      });
      formVisible.value = true;
    }
  } catch (e) {
    message.error('获取图片数据失败');
  }
};
const formData = reactive<API.PictureUploadByBatchRequest>({
  count: 10,
})
const loading = ref(false)

// 初始化
onMounted(() => {
  getTagCategoryOptions();
  getOldPicture();
});
</script>

<template>
  <div class="cosmic-container">
    <div class="gradient-background"></div>

    <div class="content-wrapper">
      <div class="header-glow"></div>

      <main class="cyber-card">
        <h1 class="neon-title">
          <span class="gradient-text">批量图片创建</span>
          <div class="accent-line"></div>
        </h1>

        <a-form
            layout="vertical"
            :model="formData"
            @finish="handleSubmit"
            class="holographic-form"
        >
          <!-- 搜索关键词 -->
          <a-form-item
              label="🔍 搜索关键词"
              name="searchText"
              class="cyber-input-item"
          >
            <a-input
                v-model:value="formData.searchText"
                placeholder="输入图片主题关键词"
                class="neon-input"
                allow-clear
            />
          </a-form-item>

          <!-- 抓取数量 -->
          <a-form-item
              label="🖼️ 抓取数量"
              name="count"
              class="cyber-input-item"
          >
            <a-input-number
                v-model:value="formData.count"
                :min="1"
                :max="30"
                class="neon-input numeric-input"
            />
            <span class="input-tip">（1-30之间）</span>
          </a-form-item>

          <!-- 名称前缀 -->
          <a-form-item
              label="🏷️ 名称前缀"
              name="namePrefix"
              class="cyber-input-item"
          >
            <a-input
                v-model:value="formData.namePrefix"
                placeholder="输入名称前缀"
                class="neon-input"
                allow-clear
            />
            <div class="example-text">示例：输入"风景_" → 生成 风景_1, 风景_2...</div>
          </a-form-item>

          <!-- 提交按钮 -->
          <a-form-item>
            <button
                type="submit"
                class="cyber-button"
                :class="{ loading }"
            >
              <span class="button-content">
                <span v-if="!loading">🚀 立即生成</span>
                <span v-else>⚡ 生成中...</span>
              </span>
              <span class="glow"></span>
            </button>
          </a-form-item>
        </a-form>
      </main>
    </div>
  </div>
</template>

<style scoped lang="less">
@keyframes gradientFlow {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.cosmic-container {
  min-height: 100vh;
  position: relative;
  overflow: hidden;
}

.gradient-background {
  position: fixed;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(45deg, #0f0c29, #302b63, #24243e);
  animation: gradientFlow 15s ease infinite;
  z-index: 0;
}

.content-wrapper {
  position: relative;
  max-width: 800px;
  margin: 0 auto;
  padding: 2rem;
  z-index: 1;
}

.header-glow {
  position: absolute;
  top: -200px;
  left: 50%;
  width: 600px;
  height: 400px;
  background: radial-gradient(rgba(99, 102, 241, 0.15), transparent 70%);
  filter: blur(80px);
  transform: translateX(-50%);
}

.cyber-card {
  background: rgba(16, 18, 27, 0.6);
  backdrop-filter: blur(12px);
  border-radius: 20px;
  padding: 2.5rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.3);
  position: relative;
  overflow: hidden;
  transition: transform 0.3s ease;

  &:hover {
    transform: translateY(-5px);
  }
}

.neon-title {
  text-align: center;
  margin-bottom: 2.5rem;
  position: relative;

  .gradient-text {
    background: linear-gradient(45deg, #8e2de2, #4a00e0);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    font-size: 2.4rem;
    font-weight: 700;
    letter-spacing: 1px;
  }

  .accent-line {
    width: 120px;
    height: 3px;
    background: linear-gradient(90deg, transparent, #8e2de2, transparent);
    margin: 1rem auto;
  }
}

.holographic-form {
  :deep(.ant-form-item-label) {
    label {
      color: rgba(255, 255, 255, 0.85);
      font-size: 1.1rem;
      font-weight: 500;
    }
  }
}

.neon-input {
  background: rgba(255, 255, 255, 0.05) !important;
  border: 1px solid rgba(255, 255, 255, 0.1) !important;
  border-radius: 8px !important;
  color: #fff !important;
  padding: 12px 16px;
  transition: all 0.3s ease;

  &:hover {
    border-color: #8e2de2 !important;
    box-shadow: 0 0 15px rgba(142, 45, 226, 0.2) !important;
  }

  &:focus {
    border-color: #4a00e0 !important;
    box-shadow: 0 0 20px rgba(74, 0, 224, 0.3) !important;
  }

  &::placeholder {
    color: rgba(255, 255, 255, 0.4) !important;
  }
}

.numeric-input {
  width: 120px;
}

.input-tip {
  color: rgba(255, 255, 255, 0.6);
  font-size: 0.9rem;
  margin-left: 1rem;
}

.example-text {
  color: rgba(255, 255, 255, 0.5);
  font-size: 0.85rem;
  margin-top: 8px;
}

.cyber-button {
  position: relative;
  width: 100%;
  padding: 16px 32px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(45deg, #8e2de2, #4a00e0);
  color: white;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  overflow: hidden;
  transition: all 0.4s ease;

  .button-content {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
  }

  .glow {
    position: absolute;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 60%);
    opacity: 0;
    transition: opacity 0.3s ease;
  }

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(142, 45, 226, 0.4);

    .glow {
      opacity: 1;
    }
  }

  &:active {
    transform: translateY(1px);
  }

  &.loading {
    background: linear-gradient(45deg, #4a00e0, #2b0066);
    cursor: not-allowed;
  }
}

@media (max-width: 768px) {
  .content-wrapper {
    padding: 1rem;
  }

  .cyber-card {
    padding: 1.5rem;
  }

  .neon-title .gradient-text {
    font-size: 2rem;
  }
}
</style>
